//修改用户信息
import { Toast } from 'antd-mobile'
const errToast = (msg) => {
    Toast.info(`${msg}`, 1)
}

const modifyuser = (user_id, user_name, user_sign, File, props) => {
    const formData = new FormData()
    formData.append('user_id', user_id)
    formData.append('user_name', user_name)
    formData.append('user_sign', user_sign)

    if (File[0].file == undefined) {
        formData.append('File', File[0].url)
    } else {
        formData.append('File', File[0].file)
    }

    return (dispatch) => {
        console.log(formData)
        let url = 'http://www.h5weixin.club:1024/api/user'
        fetch(url, {
            method: 'PUT',
            mode: 'cors',
            body: formData
        }).then((res) => {
            // console.log(res)
            return res.json()
        }).then((res) => {
            errToast(res.msg)
            console.log(res)
            localStorage.setItem('user', JSON.stringify(res.data[0]))
            dispatch({
                type: 'ADD_UR',
                value: res.data[0]
            })
            props.history.goBack()
        }).catch((err) => {
            console.log(err);
        })

    }
}

const modifysoft = (user_id, user_password_old, user_password_new, user_password_en, props) => {
    const formData = new FormData()
    formData.append('user_id', user_id)
    formData.append('password', user_password_old)
    formData.append('user_password', user_password_new)
    formData.append('password_confim', user_password_en)

    return (dispatch) => {
        console.log(formData)
        let url = 'http://www.h5weixin.club:1024/api/user'
        fetch(url, {
            method: 'PUT',
            mode: 'cors',
            body: formData
        }).then((res) => {
            return res.json()
        }).then((res) => {
            errToast(res.msg)
            localStorage.setItem('user', JSON.stringify(res.data[0]))
            dispatch({
                type: 'ADD_UR',
                value: res.data[0]
            })
            props.history.goBack()
        }).catch((err) => {
            console.log(err);
        })
    }
}

const modifybind = (user_id, user_email, business_password, props) => {
    const formData = new FormData()
    formData.append('user_id', user_id)
    // formData.append('user_phone', user_phone)
    formData.append('user_email', user_email)
    formData.append('password', business_password)
    // formData.append('password_confim', business_password_en)

    return (dispatch) => {
        console.log(formData)
        let url = 'http://www.h5weixin.club:1024/api/user'
        fetch(url, {
            method: 'PUT',
            mode: 'cors',
            body: formData
        }).then((res) => {
            return res.json()
        }).then((res) => {
            errToast(res.msg)
            localStorage.setItem('user', JSON.stringify(res.data[0]))
            dispatch({
                type: 'ADD_UR',
                value: res.data[0]
            })
            props.history.goBack()
        }).catch((err) => {
            console.log(err);
        })
    }
}

export { modifyuser, modifysoft, modifybind }